﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GoodsDragItem : UIDragDropItem
{
    /// <summary>
    /// 拖放时释放触发
    /// </summary>
    /// <param name="surface"></param>
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);

        Goods goods = original.GetComponent<Goods>();
        Package package = surface.GetComponent<Package>();
        UISprite sprite = original.GetComponent<UISprite>();
        if (goods.goodsType==package.goodsType||package.goodsType==GoodsType.EveryThing)
        {
            Goods ChildGoods = package.GetComponentInChildren<Goods>();
            Package goodsPackage = goods.GetComponentInParent<Package>();
            if (ChildGoods!=null)
            {
                UISprite Childsprite = ChildGoods.GetComponent<UISprite>();
                int j = ChildGoods.GetComponent<Goods>().i;
                if (ChildGoods.goodsType!=goodsPackage.goodsType&&goodsPackage.goodsType!=GoodsType.EveryThing)
                {
                    original.SetActive(true);
                    GoodsController.controller.DisableCollider(original.GetComponent<Goods>(), true);
                    return;
                }
                ChildGoods.transform.SetParent(goodsPackage.transform);
                ChildGoods.transform.localPosition = Vector3.zero;
                ChildGoods.gameObject.SetActive(false);
                ChildGoods.gameObject.SetActive(true);
                if (ChildGoods.goodsType == goodsPackage.goodsType)
                {
                    ChildGoods.transform.localScale = Vector3.one * 0.9f;
                    Childsprite.spriteName = HuanChange.huan.Newsprite[j];
                }
                else if (goodsPackage.goodsType == GoodsType.EveryThing)
                {
                    ChildGoods.transform.localScale = Vector3.one;
                    Childsprite.spriteName = HuanChange.huan.Originsprite[j];
                }
            }

            original.transform.SetParent(surface.transform);
            original.transform.localPosition = Vector3.zero;
            if (goods.goodsType == package.goodsType)
            {
                original.transform.localScale = Vector3.one * 0.9f;
                sprite.spriteName =HuanChange.huan.Newsprite[goods.i];
            }
            else 
            {
                original.transform.localScale = Vector3.one;
                sprite.spriteName = HuanChange.huan.Originsprite[goods.i];
            }

        }
        original.SetActive(true);
        GoodsController.controller.DisableCollider(original.GetComponent<Goods>(), true);

        //删除物品
        if (package.goodsType == GoodsType.Delete)
        {
            GoodsController.controller.DeleteGoods(goods);
        }
    }

    public Transform TopPanel;
    protected override void OnDragDropStart()
    {
        base.OnDragDropStart();

        transform.SetParent(TopPanel);
        original.SetActive(false);
        GoodsController.controller.DisableCollider(original. GetComponent<Goods>(),false);
    }

    public GameObject original;
    protected override void OnClone(GameObject original)
    {
        base.OnClone(original);

        this.original = original;
    }
}
